import time
import logging
from functools import wraps


def log_decorator(func):
    logging.basicConfig(level=logging.INFO)

    @wraps(func)
    def wrapper(*args, **kwargs):
        logging.info(f'Running "{func.__name__}" with arguments {args} and kwargs {kwargs}')  #1
        result = func(*args, **kwargs) #2     此处的func(*args, **kwargs)是timer_decorator(func)即timer_decorator(add)
        logging.info(f'Finished "{func.__name__}" with result {result}') #7
        return result

    return wrapper


def timer_decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        start_time = time.time()  #3
        result = func(*args, **kwargs) #4 调用add(5, 6)
        end_time = time.time()  #6
        print(f'Function "{func.__name__}" took {end_time - start_time} seconds to run.') #5
        return result

    return wrapper

#Python 允许我们将多个装饰器应用到一个函数上，形成一个装饰器链。例如，我们可以同时应用日志装饰器和性能测试装饰器：
#@log_decorator 和 @timer_decorator 两个装饰器被同时应用到 add 函数上，它们分别负责记录日志和测量函数运行时间。
@log_decorator
@timer_decorator
def add(x, y): #5
    time.sleep(2)
    return x + y


if __name__ == "__main__":
    add(5,6)